package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationEs;
import cn.wolfcode.luowowo.search.domain.StrategyEs;
import cn.wolfcode.luowowo.search.domain.TravelEs;
import cn.wolfcode.luowowo.search.domain.UserInfoEs;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.ISearchService;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ISearchService searchService;

    @GetMapping("/q")
    public Object query(SearchQueryObject qo) throws Exception {
        String keyword = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(keyword);
        switch (qo.getType()){
            case SearchQueryObject.TYPE_DEST:
               return  searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY:
               return  searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL:
               return  searchTravel(qo);
            case SearchQueryObject.TYPE_USER:
               return  searchUser(qo);
            default:
               return  searchAll(qo);
        }

    }

    //查询所有
    private Object searchAll(SearchQueryObject qo) {
        SearchResult searchResult = new SearchResult();
        long total=0L;
        //查询用户
       /* Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "info", "city");*/
        Page<UserInfo> us = this.getUser(qo);
        total+=us.getTotalElements();
        //查询游记
        /*Page<Travel> ts =searchService.searchWithHighlight(TravelEs.INDEX_NAME,TravelEs.TYPE_NAME,Travel.class,qo,
                "title" ,"summary");*/
        Page<Travel> ts = this.getTravel(qo);
        total+=ts.getTotalElements();
        //查询攻略
        /*Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subTitle", "summary");*/
        Page<Strategy> sts = this.getStrategy(qo);
        total+=sts.getTotalElements();
        //查询目的地
       /* Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo,
                "name","info");*/
        Page<Destination> ds = this.getDestination(qo);
        total+=ds.getTotalElements();
        searchResult.setUsers(us.getContent());
        searchResult.setTravels(ts.getContent());
        searchResult.setStrategys(sts.getContent());
        searchResult.setDests(ds.getContent());
        searchResult.setTotal(total);
        /*Map<String, Object> map = new HashMap<>();
        map.put("result",searchResult);
        map.put("qo",qo);*/
        return JsonResult.success(new ParamMap().put("result",searchResult).put("qo",qo));
    }

    //查询用户
    private Object searchUser(SearchQueryObject qo) {
        /*Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "info", "city");*/
        Page<UserInfo> page = this.getUser(qo);
       /* Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);*/
        return JsonResult.success(new ParamMap().put("page",page).put("qo",qo));
    }

    //查询游记
    private Object searchTravel(SearchQueryObject qo) {
       /* Page<Travel> page =searchService.searchWithHighlight(TravelEs.INDEX_NAME,TravelEs.TYPE_NAME,Travel.class,qo,
                "title" ,"summary");*/
        Page<Travel> page = this.getTravel(qo);
        /*Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);*/
        return JsonResult.success(new ParamMap().put("page",page).put("qo",qo));
    }

    //查询攻略
    private Object searchStrategy(SearchQueryObject qo) {
       /* Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subTitle", "summary");*/
       /* Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);*/
        Page<Strategy> page = this.getStrategy(qo);
        return JsonResult.success(new ParamMap().put("page",page).put("qo",qo));
    }

    //查询目的地
   private Object searchDest(SearchQueryObject qo) {
        //根据keyword查询出目的地名字
        //判断目的地是否存在
        //存在，查询目的地下所有的攻略，游记，用户
        //不存在，给出提示
        SearchResult searchResult = new SearchResult();
        Destination destination=destinationService.findByName(qo.getKeyword());
        if(destination!=null){
            List<Strategy> strategys=strategyService.findByDestName(qo.getKeyword());
            List<Travel>  travels=travelService.findByDestName(qo.getKeyword());
            List<UserInfo> users=userInfoService.findByDestName(qo.getKeyword());
            searchResult.setStrategys(strategys);
            searchResult.setTravels(travels);
            searchResult.setUsers(users);
            searchResult.setTotal(strategys.size()+travels.size()+users.size()+0L);
        }
        /*Map<String,Object> map=new HashMap<>();
        map.put("result",searchResult);
        map.put("dest",destination);
        map.put("qo",qo);*/
        return JsonResult.success(new ParamMap().put("result",searchResult).put("dest",destination).put("qo",qo));
    }

    //用户全文搜索
    private Page<UserInfo> getUser(SearchQueryObject qo){
        return searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "info", "city");
    }
    //攻略全文搜索
    private Page<Strategy> getStrategy(SearchQueryObject qo){
        return searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subTitle", "summary");
    }
    //游记全文搜索
    private Page<Travel> getTravel(SearchQueryObject qo){
        return searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title","summary");
    }
    //目的地全文搜索
    private Page<Destination> getDestination(SearchQueryObject qo){
        return searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo,
                "name","info");
    }


}
